package com.zjj.spring.read.xml.aop.proxy.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxy<T> implements InvocationHandler{

	private Object target;
	public DynamicProxy(Object obj) {
		target = obj;
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println(" --- DynamicProxy invoke : before ---");
		Object object = method.invoke(target, args);
		System.out.println(" --- DynamicProxy invoke : after ---");
		return "[JDK代理对返回值做的处理] - 真实返回值如下：[" + object + "]";
	}

	@SuppressWarnings({ "unchecked", "hiding" })
	public <T> T newProxy() {
		return (T) Proxy.newProxyInstance(
					target.getClass().getClassLoader(), 
					target.getClass().getInterfaces(), 
					this);
	}
	
}
